﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace Thksoft.Module.BillManagement
{
    public partial class BankCheck : BasePage
    {
        private YX_BankCheckBLL bbbll = new YX_BankCheckBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindBank();
                txtDate.Value = "";

                txtEnd.Value = "";

                ViewState["SortOrder"] = "YXBC.N_BANKCHECK_ID";
                ViewState["OrderDire"] = " desc ";

                BindData(true);
            }
        }

        private void Bind()
        {
            string whereStr = "";

            //出票公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " AND YXBC.N_BANKCHECK_CMYID= " + FORM_COMPANY_ID.Value;
            }
            else
            {
                whereStr += @"  and YXBC.N_BANKCHECK_CMYID in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            //出票银行
            if (Convert.ToDecimal(FORM_BANK_ID.SelectedValue.ToString()) != -1)
            {
                whereStr += " AND YXBC.N_BANKCHECK_BANKID = " + Convert.ToDecimal(FORM_BANK_ID.SelectedValue.ToString());
            }
            //收款人
            if (txtNAMEPAYEE.Text.Trim().Trim() != "")
            {
                whereStr += " AND C_BANKCHECK_NAMEPAYEE LIKE '%" + txtNAMEPAYEE.Text.Trim() + "%'";
            }
            //申请人
            if (txtNAMEUSER.Text.Trim() != "")
            {
                whereStr += " AND USER_NAME LIKE '%" + txtNAMEUSER.Text.Trim() + "%'";
            }

            //出票日期
            if (txtDate.Value.Trim() != "" && txtEnd.Value.Trim() != "")
            {
                whereStr += @"   and D_BANKCHECK_DATE between
       TO_DATE('" + txtDate.Value + @"', 'YYYY-MM-DD') and
       TO_DATE('" + txtEnd.Value + @"', 'YYYY-MM-DD') ";
            }

            ViewState["Where"] = whereStr;

            BindData(true);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //点击排序
            string sPage = e.SortExpression;

            if (sPage == "")
            {
                return;
            }
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
                ViewState["OrderDire"] = "ASC";
            }

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = " AND YXBC.N_BANKCHECK_CMYID= " + FORM_COMPANY_ID.Value;
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = bbbll.GetRecordCount(Where);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = bbbll.GetListByPage(Where, sort, PageIng.CurrentPage, PageIng.RowsPerPage);

            var row = dt.NewRow();
            row["N_BANKCHECK_ID"] = DBNull.Value;
            row["CMY_NAME"] = "合计";
            row["C_NAME"] = DBNull.Value;
            row["C_BANKCHECK_NAMEPAYEE"] = DBNull.Value;
            row["USER_NAME"] = DBNull.Value;
            row["D_BANKCHECK_DATE"] = DBNull.Value;
            row["N_BANKCHECK_MONEY"] = dt.AsEnumerable().Sum(a => Convert.ToDecimal(a["N_BANKCHECK_MONEY"]));
            row["N_BANKCHECK_TYPE"] = DBNull.Value;
            row["C_BANKCHECK_REMARK"] = DBNull.Value;
            dt.Rows.Add(row);

            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                grvShow1.DataSource = null;
                grvShow1.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow1.Rows.Count; i++)
            {
                if (((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).ToolTip;
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaoChu_Click(object sender, EventArgs e)
        {
            var dt = (DataTable)ViewState["database"];
            dt.Columns["C_BANKCHECK_NAMEPAYEE"].ColumnName = "收款人";
            dt.Columns["USER_NAME"].ColumnName = "申请人";
            dt.Columns["D_BANKCHECK_DATE"].ColumnName = "出票日期";
            dt.Columns["N_BANKCHECK_MONEY"].ColumnName = "出票金额";
            dt.Columns["N_BANKCHECK_TYPE"].ColumnName = "支付类型";
            dt.Columns["C_BANKCHECK_REMARK"].ColumnName = "备注";
            dt.Columns["CMY_NAME"].ColumnName = "公司名称";
            dt.Columns["ACCOUNTNO"].ColumnName = "支付账号";
            dt.Columns["C_NAME"].ColumnName = "支付银行";
            dt.Columns.Remove("N_BANKCHECK_ID");
            dt.Columns.Remove("r");
            toExecl1(dt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "银行本票");
        }

        /// <summary>
        /// 得到汇票银行
        /// </summary>
        public void BindBank()
        {
            int count;

            var dt = new Sys_BankBLL().GetEntitys(null, null, 0, 1000, out count);

            FORM_BANK_ID.DataSource = dt;
            FORM_BANK_ID.DataTextField = "C_NAME";
            FORM_BANK_ID.DataValueField = "N_ID";
            FORM_BANK_ID.DataBind();
            FORM_BANK_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));

            FORM_BANK_ID.SelectedValue = "-1";
        }
    }
}